package pers.cz.testNg;

import com.jayway.jsonpath.JsonPath;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.testng.Reporter;
import pers.cz.expression.JefCustomErrorListener;
import pers.cz.expression.JefCustomVisitor;
import pers.cz.expression.JefExpressionLexer;
import pers.cz.expression.JefExpressionParser;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: PostGril_1.0
 * @description: MyCheck
 * @author: Cheng Zhi
 * @create: 2021-11-08 16:23
 **/
public class MyCheck {

    /**
     * 异常检查点
     * @param actual 实际的
     * @param expected 预期的
     */
    public static Boolean exceptionCheck(Object actual, Object expected) {

        return false;
    }

    /**
     * 不为空检查点
     * @param actual 实际的
     * @param expected 预期的
     */
    public static Boolean notNullCheck(Object actual, Object expected) {

        Boolean isPass = true;
        if (actual.toString().contains("") || actual.toString().contains("null") || actual.toString().contains("NULL")) {
            throw new RuntimeException("实际结果存在空值");
        }
        Reporter.log("不为空点检查通过");
        return isPass;
    }

    /**
     * 包含检查点
     * @param actual 实际的
     * @param expected 预期的
     */
    public static Boolean contentCheck(Object actual, Object expected) {

        Boolean isPass = true;
        if (actual.toString().contains(expected.toString())) {

            Reporter.log("包含点检查通过");
        } else {
            throw new RuntimeException("实际结果中不包含预期结果");
        }
        return isPass;
    }

    /**
     * jsonPath检查点
     * @param actual 实际的
     * @param expected 检查表达式
     */
    public static Boolean jsonPathCheck(String actual, String expected) {

        Boolean o = PostGirlParser.blockLexer(actual, expected);

        if (o) {
            Reporter.log("jsonPath检查通过");
        } else {
            throw new RuntimeException("jsonPath检查不通过");
        }

        return o;
    }

    /**
     * 自定义表达验证
     * @param beforeDataBaseResult
     * @param afterDataBaseResult
     * @param interfaceRequest
     * @param interfaceResponse
     * @param expected
     * @return
     */
    public static Boolean customExprCheck(Map<String, List<Map<String, Object>>> beforeDataBaseResult, Map<String, List<Map<String, Object>>> afterDataBaseResult, String interfaceRequest, String interfaceResponse, String expected) {

        JefExpressionLexer arrayInitLexer = new JefExpressionLexer(new ANTLRInputStream(expected));
        CommonTokenStream commonTokenStream = new CommonTokenStream(arrayInitLexer);
        JefExpressionParser parser = new JefExpressionParser(commonTokenStream);
        // 添加自定义的错误监听器到语法分析器
        parser.removeErrorListeners(); // 移除默认的错误监听器
        parser.addErrorListener(new JefCustomErrorListener());
        ParseTree tree = parser.prog();
        JefCustomVisitor jefCustomVisitor = new JefCustomVisitor(afterDataBaseResult, beforeDataBaseResult, interfaceRequest, interfaceResponse);
        jefCustomVisitor.visit(tree);
        Long leftValue = jefCustomVisitor.getLeftValue();
        Long rightValue = jefCustomVisitor.getRightValue();
        Reporter.log("自定义表达式执行结果 left:" + leftValue + " right:" + rightValue );
        if (leftValue.equals(rightValue)) {
            Reporter.log("自定义表达式检查通过");
            return true;
        } else {
            throw new RuntimeException("表达式执行结果左侧和右侧不相等");
        }
    }

}
